﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.Widgets.MindMap
{
    /// <summary>
    /// MindMapPreviewDialog.xaml 的交互逻辑
    /// </summary>
    public partial class MindMapPreviewDialog : Window
    {
        public MindMapPreviewDialog()
        {
            InitializeComponent();

            this.mindMapView.btnSnapItAndInsertToDocument.Visibility = Visibility.Collapsed;

            mindMapView.baseBoard.ToolTip = "[Esc 或 鼠标左键双击空白区域]退出预览";
            mindMapView.btnFirst.ToolTip = "[Ctrl+PageUp]显示第一个根节点";
            mindMapView.btnPreview.ToolTip = "[PageUp]显示前一个根节点";
            mindMapView.btnPreviewRootElement.ToolTip = "[Ctrl+Home]显示当前节点所在根节点";
            mindMapView.btnParentElement.ToolTip = "[Home]显示上级节点";
            mindMapView.btnNext.ToolTip = "[PageDown]显示后一个根节点";
            mindMapView.btnLast.ToolTip = "[Ctrl+PageDown]显示最后一个根节点";
            mindMapView.btnSnapIt.ToolTip = "[Ctrl+C]将当前导图复制到剪贴板";
            mindMapView.btnSaveMindMapImage.ToolTip = "[Ctrl+S]保存当前导图";

            mindMapView.btnPreviewAsListView.ToolTip = "[1]列表视图";
            mindMapView.btnPreviewAsDownOrgView.ToolTip = "[2]下开口组织结构图";
            mindMapView.btnPreviewAsLeftFish.ToolTip = "[3]左向鱼骨图";
            mindMapView.btnPreviewAsRightOrgView.ToolTip = "[4]右武器组织结构图";
            mindMapView.btnPreviewAsDefaultMindMapView.ToolTip = "[5]思维导图\r\n[0或按Ctrl并单击]全子节导图";
            mindMapView.btnPreviewAsLeftOrgView.ToolTip = "[6]左开口组织结构图";
            mindMapView.btnPreviewAsAncestorView.ToolTip = "[7]祖先节点图";
            mindMapView.btnPreviewAsUpOrgView.ToolTip = "[8]上开口组织结构图";
            mindMapView.btnPreviewAsRightFish.ToolTip = "[9]右向鱼骨图";

            mindMapView.btnShowTools.ToolTip = "[Space]显示视图切换按钮";

            mindMapView.btnDemonstratResources.ToolTip = "展示当前标题直接下级图文资源节点";
        }

        public TreeElement CurDisplayingElement { get; set; }

        private void Window_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            L.GetKeyboardState(out bool isAlt, out bool isShift, out bool isCtrl);

            this.Focus();   // 不是这个造成的

            switch (e.Key)
            {
                case Key.Escape:
                    {
                        this.Close(); break;
                    }
                case Key.PageDown:
                    {
                        if (isCtrl && !isAlt && !isShift)
                        {
                            mindMapView.ShowLastRootElement();
                        }
                        else
                        {
                            mindMapView.ShowNextRootElement();
                        }
                        e.Handled = true;
                        break;
                    }
                case Key.PageUp:
                    {
                        if (isCtrl && !isAlt && !isShift)
                        {
                            mindMapView.ShowFirstRootElement();
                        }
                        else
                        {
                            mindMapView.ShowPreviewRootElement();
                        }
                        e.Handled = true;
                        break;
                    }
                case Key.Home:
                    {
                        if (isCtrl && !isAlt && !isShift)
                        {
                            mindMapView.ShowCurrentRootElement();
                        }
                        else
                        {
                            mindMapView.ShowParentElement();
                        }
                        e.Handled = true;
                        break;
                    }
                case Key.C:
                    {
                        if (isCtrl && !isAlt && !isShift)
                        {
                            mindMapView.SnapIt();
                            e.Handled = true;
                        }
                        break;
                    }
                case Key.S:
                    {
                        if (!isCtrl && !isAlt && !isShift)
                        {
                            mindMapView.SaveMindMapImage();
                            e.Handled = true;
                        }
                        break;
                    }
                case Key.D0:
                    {
                        if (!isCtrl && !isAlt && !isShift)
                        {
                            mindMapView.PreviewAsFullMindMapView(mindMapView.btnPreviewAsDefaultMindMapView.Tag.ToString(),true);
                            e.Handled = true;
                        }
                        break;
                    }
                case Key.D1:
                    {
                        if (!isCtrl && !isAlt && !isShift)
                        {
                            mindMapView.PreviewAsListView(mindMapView.btnPreviewAsListView.Tag.ToString(), true);
                            e.Handled = true;
                        }
                        break;
                    }
                case Key.D2:
                    {
                        if (!isCtrl && !isAlt && !isShift)
                        {
                            mindMapView.PreviewAsOrgView(mindMapView.btnPreviewAsDownOrgView.Tag.ToString(), true);
                            e.Handled = true;
                        }
                        break;
                    }
                case Key.D3:
                    {
                        if (!isCtrl && !isAlt && !isShift)
                        {
                            mindMapView.PreviewAsFishBone(mindMapView.btnPreviewAsLeftFish.Tag.ToString(), true);
                            e.Handled = true;
                        }
                        break;
                    }
                case Key.D4:
                    {
                        if (!isCtrl && !isAlt && !isShift)
                        {
                            mindMapView.PreviewAsOrgView(mindMapView.btnPreviewAsRightOrgView.Tag.ToString(), true);
                            e.Handled = true;
                        }
                        break;
                    }
                case Key.D5:
                    {
                        if (!isCtrl && !isAlt && !isShift)
                        {
                            mindMapView.PreviewAsDefaultMindMapView(mindMapView.btnPreviewAsDefaultMindMapView.Tag.ToString(), true);
                            e.Handled = true;
                        }
                        break;
                    }
                case Key.D6:
                    {
                        if (!isCtrl && !isAlt && !isShift)
                        {
                            mindMapView.PreviewAsOrgView(mindMapView.btnPreviewAsLeftOrgView.Tag.ToString(), true);
                            e.Handled = true;
                        }
                        break;
                    }
                case Key.D7:
                    {
                        if (!isCtrl && !isAlt && !isShift)
                        {
                            mindMapView.PreviewAsAncestorView(mindMapView.btnPreviewAsAncestorView.Tag.ToString(), true);
                            e.Handled = true;
                        }
                        break;
                    }
                case Key.D8:
                    {
                        if (!isCtrl && !isAlt && !isShift)
                        {
                            mindMapView.PreviewAsOrgView(mindMapView.btnPreviewAsUpOrgView.Tag.ToString(), true);
                            e.Handled = true;
                        }
                        break;
                    }
                case Key.D9:
                    {
                        if (!isCtrl && !isAlt && !isShift)
                        {
                            mindMapView.PreviewAsFishBone(mindMapView.btnPreviewAsRightFish.Tag.ToString(), true);
                            e.Handled = true;
                        }
                        break;
                    }
                case Key.Space:
                    {
                        if (!isCtrl && !isAlt && !isShift)
                        {
                            if (mindMapView.spViews.Visibility == Visibility.Visible)
                            {
                                mindMapView.spViews.Visibility = Visibility.Collapsed;
                            }
                            else
                            {
                                mindMapView.spViews.Visibility = Visibility.Visible;
                            }
                            e.Handled = true;
                        }
                        break;
                    }
                case Key.Enter:
                    {
                        if (!isCtrl && !isAlt && !isShift)
                        {
                            MindMapTitle mmt = null;
                            foreach (var ue in mindMapView.MainCanvas.Children)
                            {
                                var title = ue as MindMapTitle;
                                if (title == null) continue;

                                if (title.IsSelectedTitle)
                                {
                                    mmt = title;
                                    break;
                                }
                            }

                            if (mmt != null)
                            {
                                mindMapView.DisplayMindMap(mmt.Element);
                            }
                            e.Handled = true;
                        }
                        break;
                    }
                case Key.Left:
                    {
                        if (!isCtrl && !isAlt && !isShift)
                        {
                            var selTitle = mindMapView.SelectedTitle;
                            if (selTitle == null) return;
                            var destTitle = selTitle.LeftTitle;
                            if(destTitle == null) return;
                            mindMapView.SelectedTitle = destTitle;
                            selTitle.IsSelectedTitle = false;
                            destTitle.IsSelectedTitle = true;
                        }
                        break;
                    }
                case Key.Right:
                    {
                        if (!isCtrl && !isAlt && !isShift)
                        {
                            var selTitle = mindMapView.SelectedTitle;
                            if (selTitle == null) return;
                            var destTitle = selTitle.RightTitle;
                            if (destTitle == null) return;
                            mindMapView.SelectedTitle = destTitle;
                            selTitle.IsSelectedTitle = false;
                            destTitle.IsSelectedTitle = true;
                        }
                        break;
                    }
                case Key.Up:
                    {
                        if (!isCtrl && !isAlt && !isShift)
                        {
                            var selTitle = mindMapView.SelectedTitle;
                            if (selTitle == null) return;
                            var destTitle = selTitle.TopTitle;
                            if (destTitle == null) return;
                            mindMapView.SelectedTitle = destTitle;
                            selTitle.IsSelectedTitle = false;
                            destTitle.IsSelectedTitle = true;
                        }
                        break;
                    }
                case Key.Down:
                    {
                        if (!isCtrl && !isAlt && !isShift)
                        {
                            var selTitle = mindMapView.SelectedTitle;
                            if (selTitle == null) return;
                            var destTitle = selTitle.BottomTitle;
                            if (destTitle == null) return;
                            mindMapView.SelectedTitle = destTitle;
                            selTitle.IsSelectedTitle = false;
                            destTitle.IsSelectedTitle = true;
                        }
                        break;
                    }
                // 无论是在这里还是在 MainWindow 通过 Apps 快捷键调用演示窗口总会崩溃，但通过鼠标就不会有问题。
                //case Key.Apps:
                //    {
                //        if (!isAlt && !isShift && isCtrl)
                //        {
                //            mindMapView.DemonstratResources();
                //            e.Handled = true;
                //        }
                //        break;
                //    }
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.mindMapView.DisplayMindMap(this.CurDisplayingElement);
        }

    }
}
